package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PaymentResultMapper;
import cn.wolfcode.crm.query.AccidentMapperQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.service.IEmployeeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AccidentServiceImpl implements IAccidentService {
    @Autowired
    private AccidentMapper accidentMapper;


    @Autowired
    private PaymentResultMapper paymentresultMapper;



    public void deleteByPrimaryKey(Long id) {
         accidentMapper.deleteByPrimaryKey(id);
    }

    public void insert( Accident record) {

        //将当前登录人信息录入,设置操作人
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        record.setEname("默认值");


        //根据客户的id获取的paymentresult的id
        Long clientId = record.getApplicantId();
        record.setPayId(paymentresultMapper.slectpaymentresultIdBYapplicantId(clientId));


        accidentMapper.insert(record);
    }

    public Accident selectByPrimaryKey(Long id) {
        return accidentMapper.selectByPrimaryKey(id);
    }

    public List<Accident> selectAll() {
        return accidentMapper.selectAll();
    }

    public void updateByPrimaryKey( Accident record) {

        //根据客户的id获取的paymentresult的id
        Long clientId = record.getApplicantId();
        record.setPayId(paymentresultMapper.slectpaymentresultIdBYapplicantId(clientId));
        accidentMapper.updateByPrimaryKey(record);
    }



    @Override
    public void changeStatus(long id,long status) {
        accidentMapper.changeStatus(id,status);
    }

    @Override
    public PageResult query(AccidentMapperQueryObject qo) {
        int total = accidentMapper.queryForCount(qo);

        if (total==0){
            return new PageResult();
        }
        List<Map<Object,String>> rows = accidentMapper.queryForList(qo);

        return new PageResult(rows,total);
    }

}
